package com.droidclaimcustomer.activity;


import com.droidclaimcustomer.R;

import android.os.Bundle;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {
	// private static final AuthenCustomerResultDto dto = null;
	static Resources resources;
	static TabHost tabHost;

	private static String sessionId;
	private static String licensePlate;
	private static String ownerName;
	private static String carDetail;

	private String name, surname, mobile, custImg, carModel, carImg, carColor,
			carBrand, insurancePackage;

	private Intent intentHome;
	private static Intent intentDetail;
	// private static TabSpec tabDetail;
	private String tabHomeId, tabRequestId, tabHistoryId;
	private static String tabDetailId;

	private TabSpec tabHome;
	//private static TabSpec tabDetail, tabHistory;
	private TabSpec tabClaimRequest;

	private static Intent detailIntent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		intentHome = new Intent().setClass(this, HomeActivity.class);
		intentDetail = new Intent().setClass(this, ClaimDetailActivity.class);

		// Start Initial Tab Value
		tabHomeId = "Home";
		tabRequestId = "Request";
		tabHistoryId = "History";
		tabDetailId = "Detail";
		// End Initail Tab Value

		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();

		licensePlate = bundle.getString("licensePlate");
		name = bundle.getString("name");
		surname = bundle.getString("surname");
		mobile = bundle.getString("mobile");
		custImg = bundle.getString("custImg");
		carModel = bundle.getString("carModel");
		carImg = bundle.getString("carImg");
		carColor = bundle.getString("carColor");
		carBrand = bundle.getString("carBrand");
		insurancePackage = bundle.getString("insurancePackage");

		Initail();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);

		return true;
	}

	public void Initail() {
		resources = getResources();
		tabHost = (TabHost) findViewById(android.R.id.tabhost);

		// Android tab
		TabSpec tabHome = tabHost
				.newTabSpec(tabHomeId)
				.setIndicator("",
						resources.getDrawable(R.drawable.icon_android_config))
				.setContent(getIntentHome());

		Intent intentClaimRequest = new Intent().setClass(this,
				ClaimRequestActivity.class);
		intentClaimRequest.putExtra("mobile", mobile);
		TabSpec tabClaimRequest = tabHost
				.newTabSpec(tabRequestId)
				.setIndicator("",
						resources.getDrawable(R.drawable.icon_add_config))
				.setContent(intentClaimRequest);

		Intent intentHistory = new Intent().setClass(this,
				ActivityStackHistory.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		TabSpec tabHistory = tabHost
				.newTabSpec(tabHistoryId)
				.setIndicator("",
						resources.getDrawable(R.drawable.icon_history_config))
				.setContent(intentHistory);

		Intent intentDetail = new Intent().setClass(this,
				ClaimDetailActivity.class);
		intentDetail.putExtra("request_id", "101");
		TabSpec tabDetail = tabHost
				.newTabSpec(tabDetailId)
				.setIndicator("",
						resources.getDrawable(R.drawable.icon_detail_config))
				.setContent(intentDetail);

		tabHost.addTab(tabHome);
		tabHost.addTab(tabClaimRequest);
		tabHost.addTab(tabHistory);
		tabHost.addTab(tabDetail);
		tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			@Override
			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
//				Intent intent = getIntent();
//				finish();
//				startActivity(intent);
//		        String currentTag = tabHost.getCurrentTabTag();
//		        LocalActivityManager manager = getLocalActivityManager();
//				if (tabId.equals(tabDetailId)) {
//					defaultTabDetail();
//				} else if (tabId.equals(tabHomeId)) {
//					defaultTabHome();
//				} else if (tabId.equals(tabRequestId)) {
//					defaultTabRequest();
//				} else if (tabId.equals(tabHistoryId)) {
//					manager.destroyActivity(currentTag, true);
//					Intent intent = new Intent(MainActivity.this,ClaimHistoryActivity.class);
//					intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//					manager.startActivity(currentTag ,new Intent(intent));
//					//tabHost.getCurrentTabView().setc
//					
//					defaultTabHistory();
//				}

			}

		});

		tabHost.setCurrentTab(0);
	}

	public void defaultTabHome() {
		tabHome = tabHost
				.newTabSpec(tabHomeId)
				.setIndicator("",
						resources.getDrawable(R.drawable.icon_android_config))
				.setContent(getIntentHome());
		// tabHome.setContent(getIntentHome().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
		// tabHost.setCurrentTab(0);
	}

	public void defaultTabDetail() {
//		tabDetail = tabHost
//				.newTabSpec(tabDetailId)
//				.setIndicator("",
//						resources.getDrawable(R.drawable.icon_detail_config))
//				.setContent(getIntentDetail());
	}

	public void defaultTabRequest() {
		tabClaimRequest = tabHost
				.newTabSpec(tabRequestId)
				.setIndicator("",
						resources.getDrawable(R.drawable.icon_add_config))
				.setContent(getIntentRequest());
		// tabClaimRequest.setContent(getIntentRequest().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
	}

	public void defaultTabHistory() {
//		tabHistory = tabHost
//				.newTabSpec(tabHistoryId)
//				.setIndicator("",
//						resources.getDrawable(R.drawable.icon_history_config))
//				.setContent(getIntentHitory());
	}

	public Intent getIntentHome() {
		// PutExtra Intent Home
		intentHome.putExtra("licensePlate", licensePlate);
		intentHome.putExtra("name", name);
		intentHome.putExtra("surname", surname);
		intentHome.putExtra("mobile", mobile);
		intentHome.putExtra("custImg", custImg);
		intentHome.putExtra("carModel", carModel);
		intentHome.putExtra("carImg", carImg);
		intentHome.putExtra("carColor", carColor);
		intentHome.putExtra("carBrand", carBrand);
		intentHome.putExtra("insurancePackage", insurancePackage);
		return intentHome;
	}

	public Intent getIntentDetail() {

		// getParent().getIntent().putExtra("request_id", "101");
		Intent intent = new Intent().setClass(this, ClaimDetailActivity.class);
		intent.putExtra("request_id", "101");
		return intent;
	}

	public Intent getIntentRequest() {

		Intent intent = new Intent().setClass(this, ClaimRequestActivity.class);
		// PutExtra Intent Request
		intent.putExtra("mobile", mobile);
		return intent;
	}

	public Intent getIntentHitory() {

		Intent intent = new Intent().setClass(this, ClaimHistoryActivity.class);
		// PutExtra Intent Request
		// intent.putExtra("mobile", mobile);
		return intent;
	}

//	public static void clickTabIndex(int index, Intent intent) {
//		if (index == 3) {
//			// tabDetail.setContent(intentDetail);
//			if (intent != null) {
//				;
//				// detailIntent = intent;
//				tabDetail.setContent(intent);
//			}
//		}
//		tabHost.setCurrentTab(index);
//	}

	public static String getSessionId() {
		return MainActivity.sessionId;
	}

	public static void setSessionId(String sessionId) {
		MainActivity.sessionId = sessionId;
	}

	public static String getLicensePlate() {
		return licensePlate;
	}

	public static void setLicensePlate(String licensePlate) {
		MainActivity.licensePlate = licensePlate;
	}

	public static String getOwnerName() {
		return ownerName;
	}

	public static void setOwnerName(String ownerName) {
		MainActivity.ownerName = ownerName;
	}

	public static String getCarDetail() {
		return carDetail;
	}

	public static void setCarDetail(String carDetail) {
		MainActivity.carDetail = carDetail;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub

		/*
		 * if(item.getId()==R.id.xxx) Logout Call Service.Logout() Remove
		 * SessionId >> SetEmpty
		 */
		return super.onOptionsItemSelected(item);
	}

}
